import { createRouter, createWebHashHistory } from "vue-router";

// 路由守卫函数
const requireAuth = (to, from, next) => {
    const userInfo = localStorage.getItem("userInfo");
    if (userInfo) {
        next();
    } else {
        next("/login");
    }
};

const requireAdmin = (to, from, next) => {
    const userInfo = localStorage.getItem("userInfo");
    if (userInfo) {
        const user = JSON.parse(userInfo);
        if (user.role === "admin") {
            next();
        } else {
            next("/user/home");
        }
    } else {
        next("/login");
    }
};

const router = createRouter({
    history: createWebHashHistory(),
    routes: [
        {
            path: "/",
            children: [
                {
                    path: "",
                    name: "home",
                    component: () => import("../views/user/Home.vue"),
                },
                // {
                //     path: "about",
                //     name: "about",
                //     component: () => import("../views/user/About.vue"),
                // },
                // {
                //     path: "services",
                //     name: "services",
                //     component: () => import("../views/user/Services.vue"),
                // },
                // {
                //     path: "contact",
                //     name: "contact",
                //     component: () => import("../views/user/Contact.vue"),
                // },
                // {
                //     path: "message",
                //     name: "message",
                //     component: () => import("../views/user/Message.vue"),
                // },
            ],
        },
        {
            path: "/login",
            name: "login",
            component: () => import("../views/Login.vue"),
            beforeEnter: (to, from, next) => {
                const userInfo = localStorage.getItem("userInfo");
                if (userInfo) {
                    next("/system/home");
                } else {
                    next();
                }
            },
        },
        {
            path: "/system",
            component: () => import("../components/Layout.vue"),
            beforeEnter: requireAuth,
            children: [
                {
                    path: "home",
                    name: "system-home",
                    component: () => import("../views/user/Home.vue"),
                },
                {
                    path: "personal-info",
                    name: "personal-info",
                    component: () => import("../views/personal/PersonalInfo.vue"),
                },
                {
                    path: "edit-personal-info",
                    name: "edit-personal-info",
                    component: () => import("../views/personal/EditPersonalInfo.vue"),
                },
                {
                    path: "visitor-records",
                    name: "visitor-records",
                    component: () => import("../views/personal/VisitorRecords.vue"),
                },
                {
                    path: "apply-visit",
                    name: "apply-visit",
                    component: () => import("../views/personal/ApplyVisit.vue"),
                },
                {
                    path: "health-records",
                    name: "health-records",
                    component: () => import("../views/personal/HealthRecords.vue"),
                },
                {
                    path: "medical-medication",
                    name: "medical-medication",
                    component: () => import("../views/personal/MedicalMedication.vue"),
                },
                {
                    path: "family-relations",
                    name: "family-relations",
                    component: () => import("../views/personal/FamilyRelationships.vue"),
                },
                {
                    path: "outing-records",
                    name: "outing-records",
                    component: () => import("../views/personal/OutingRecords.vue"),
                },
            ],
        },
    ],
});

export default router;
